package com.hc.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hc.music.common.R;
import com.hc.music.dao.UserUpMapper;
import com.hc.music.entity.UserUp;
import com.hc.music.service.UserUpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserUpServiceImpl extends ServiceImpl<UserUpMapper, UserUp> implements UserUpService {
    @Autowired
    private UserUpMapper userUpMapper;

    @Override
    public R insertOrDeleteUp(UserUp userUp) {
        QueryWrapper<UserUp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id", userUp.getCommentId());
        queryWrapper.eq("user_id", userUp.getUserId());
        if (userUpMapper.selectCount(queryWrapper) > 0) {
            //删除点赞
            if (userUpMapper.delete(queryWrapper) > 0) {
                return R.success("取消点赞成功",false);
            }else {
                return R.success("取消点赞失败");
            }
        }else {
            //新增点赞
            if(userUpMapper.insert(userUp) > 0){
                return R.success("点赞成功",true);
            }else {
                return R.error("点赞失败");
            }
        }
    }
}
